package com.budwk.app.sqfwV2.generator.weiqingmen;

import cn.hutool.core.date.DateUtil;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.CommonParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.fileParam.PdfGenerateParam;
import com.budwk.app.sqfwDoc.codeGeneratorV2.generate.BasePdfGenerator;
import com.budwk.app.sqfwV2.dto.weiqingmen.SafetyCheckDTO;
import com.budwk.app.util.ClassUtil;
import com.budwk.app.util.poiUtils.PictureUtils;
import com.deepoove.poi.XWPFTemplate;
import com.deepoove.poi.xwpf.NiceXWPFDocument;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;
import java.util.Map;

/**
 * @author 东方祥云低代码1.1
 * @since 2024-04-01
 * <p>
 * 威清门消防安全排查 SafetyCheck
 * 安全生产整改通知书.docx
 */
public class SafeProductionPdfGenerator extends BasePdfGenerator {

    /**
     * 判断使用使用这个generator生成pdf
     *
     * @param pdfDTO 创建pdf数据素材
     * @return 结果
     */
    @Override
    public boolean pdfCreateConditionCheck(PdfGenerateParam pdfDTO) {
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfDTO.getAbstractCustomerPdfCreateField();
        boolean createFlag = false;
        if (fieldDTO.getAttributesOfInspectionUnits().contains("重点场所") && !"无".equals(BaseGenerator.getHiddenDangers(fieldDTO))) {
            createFlag = true;

        }
        return createFlag;
    }

    @Override
    public String getTemplatePath(PdfGenerateParam pdfGenerateParam) {
        return "sqfwV2/weiqingmen/SafeProduction.docx";
    }

    @Override
    public void customerPopularPdfParam(Map<String, Object> paramMap, PdfGenerateParam pdfGenerateParam) {
        // 在这里对一些数据进行填充 进行复杂的业务代码逻辑
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        // 创建人的信息
        CommonParam createUserCommonParam = pdfGenerateParam.getCreateUserCommonParam();
        if (StringUtils.isNotBlank(createUserCommonParam.getCreateUserSigUrl())) {
            paramMap.put(ClassUtil.getField(CommonParam::getCreateUserName), "");
        }

        // 巡查时间
        Date inspectionDate = fieldDTO.getInspectionDate();
        paramMap.put("yyyy", DateUtil.year(inspectionDate));
        paramMap.put("mm", DateUtil.month(inspectionDate) + 1);//月份从0开始,所以要加1
        paramMap.put("dd", DateUtil.dayOfMonth(inspectionDate));

        // 巡查问题（只显示限期整改问题,同时不显示其他问题）
        paramMap.put("hiddenDangers", BaseGenerator.getHiddenDangers(fieldDTO, "限期整改", "notOther"));

        // 整改完成时间
        Date deadlineForRectification = fieldDTO.getDeadlineForRectification();
        paramMap.put("cyyyy", DateUtil.year(deadlineForRectification));
        paramMap.put("cmm", DateUtil.month(deadlineForRectification) + 1);//月份从0开始,所以要加1
        paramMap.put("cdd", DateUtil.dayOfMonth(deadlineForRectification));
    }

    /**
     * 签名、盖章
     * @param xwpfTemplate         XWPDF文件
     * @param pdfGenerateParam 生成pdf的参数
     */
    protected void createXWPDFTemplateAfter(XWPFTemplate xwpfTemplate, PdfGenerateParam pdfGenerateParam) {
        CommonParam createUserCommonParam = pdfGenerateParam.getCreateUserCommonParam();
        SafetyCheckDTO fieldDTO = (SafetyCheckDTO) pdfGenerateParam.getAbstractCustomerPdfCreateField();

        NiceXWPFDocument xwpfDocument = xwpfTemplate.getXWPFDocument();
        try {
            xwpfDocument = PictureUtils.localImageSeal(xwpfDocument, "盖章", PictureUtils.getNewLocalImagePath(createUserCommonParam.getLevelCityUnitName()),
                    280, -8, 125);
            PictureUtils.networkImageSeal(xwpfDocument, "签收人签名", fieldDTO.getCheckedUnitLeader(),
                    130, -5, 65);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
